package com.gitee.qdbp.filling.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.gitee.qdbp.able.beans.KeyString;
import com.gitee.qdbp.filling.api.EntityDataFillService;
import com.gitee.qdbp.filling.api.EntityDataTypedFillService;

/**
 * 实体数据处理类基础实现类
 *
 * @author zhaohuihua
 * @version 20210113
 */
public abstract class BaseEntityDataFillService implements EntityDataFillService {

    @Override
    public void fillFieldValue(Iterable<Map<String, Object>> data, String... options) {
        List<KeyString> list = parseOptions(options);
        for (KeyString item : list) {
            EntityDataTypedFillService service = findService(item.getKey());
            if (service == null) {
                continue;
            }
            service.fillFieldValue(data, item.getValue());
        }
    }

    protected abstract EntityDataTypedFillService findService(String type);

    protected List<KeyString> parseOptions(String... options) {
        List<KeyString> list = new ArrayList<>();
        for (String option : options) {
            option = option.trim();
            int index = option.indexOf(':');
            if (index < 0) {
                list.add(new KeyString(option, null));
            } else if (index > 0) {
                String type = option.substring(0, index).trim();
                String args = option.substring(index + 1).trim();
                list.add(new KeyString(type, args));
            // } else {
                // 冒号开头? 忽略!
            }
        }
        return list;
    }
}
